package com.it.nongchanping.service.impl;

import com.it.nongchanping.entity.Product;
import com.it.nongchanping.mapper.ProductMapper;
import com.it.nongchanping.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 产品服务实现类
 */
@Service
public class ProductServiceImpl implements ProductService {
    
    @Autowired
    private ProductMapper productMapper;
    
    @Override
    public List<Product> getAllProducts() {
        return productMapper.findAll();
    }
    
    @Override
    public Product getProductById(Long id) {
        return productMapper.findById(id);
    }
    
    @Override
    public List<Product> getProductsByCategory(String category) {
        return productMapper.findByCategory(category);
    }
    
    @Override
    public List<Product> getAvailableProducts() {
        return productMapper.findByStatus("上架");
    }
    
    @Override
    public boolean saveProduct(Product product) {
        product.setCreateTime(LocalDateTime.now());
        product.setUpdateTime(LocalDateTime.now());
        return productMapper.insert(product) > 0;
    }
    
    @Override
    public boolean updateProduct(Product product) {
        product.setUpdateTime(LocalDateTime.now());
        return productMapper.update(product) > 0;
    }
    
    @Override
    public boolean deleteProduct(Long id) {
        return productMapper.deleteById(id) > 0;
    }
    
    @Override
    public List<Product> searchProducts(String keyword) {
        return productMapper.search(keyword);
    }

    @Override
    public long countAllProducts() {
        return productMapper.countAll();
    }
} 